/**
 * 
 */
package com.android.contacts;

import java.util.ArrayList;

import android.content.ContentResolver;
import android.content.Intent;
import android.database.Cursor;
import android.util.Log;

import com.android.contacts.model.vo.Address;
import com.android.contacts.model.vo.Contact;
import com.android.contacts.model.vo.ContactList;
import com.android.contacts.model.ContactsConstants;
import com.android.contacts.model.vo.Email;
import com.android.contacts.model.vo.IM;
import com.android.contacts.model.vo.Organization;
import com.android.contacts.model.vo.Phone;
import com.android.utility.Utils;

/**
 * @author Amit
 * 
 */
public class ContactsApiSdk5 extends ContactAPI {

	private Cursor mCursor;
	private ContentResolver mContentResolver;
	private ContactsBackCallback mContactsBackCallback;

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.android.contacts.ContactAPI#getContactIntent()
	 */
	@Override
	public Intent getContactIntent() {
		return (new Intent(Intent.ACTION_PICK, ContactsConstants.URI));
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.android.contacts.ContactAPI#getContentResolver()
	 */
	@Override
	public ContentResolver getContentResolver() {
		return mContentResolver;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.android.contacts.ContactAPI#getCursor()
	 */
	@Override
	public Cursor getCursor() {
		return mCursor;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @seecom.android.contacts.ContactAPI#setContentResolver(android.content.
	 * ContentResolver)
	 */
	@Override
	public void setContentResolver(ContentResolver contentResolver) {
		this.mContentResolver = contentResolver;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.android.contacts.ContactAPI#setCusror(android.database.Cursor)
	 */
	@Override
	public void setCusror(Cursor cursor) {
		this.mCursor = cursor;

	}

	public interface ContactsBackCallback {
		public void saveContact(final String msg);
	}

	public void setContactsBackCallback(ContactsBackCallback callback) {
		this.mContactsBackCallback = callback;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.android.contacts.ContactAPI#getContactList()
	 */
	@Override
	public ContactList getContactList() {
		if (mContentResolver == null) {
			return null;
		}
		ContactList contactList = new ContactList();
		String id;
		this.mCursor = this.mContentResolver.query(ContactsConstants.URI, null,
				null, null, null);
		if (this.mCursor.getCount() > 0) {
			while (this.mCursor.moveToNext()) {
				Contact contact = new Contact();
				id = Utils.getColumnValue(mCursor, ContactsConstants.ID);
				contact.setId(id);
				contact.setDisplayName(Utils.getColumnValue(mCursor,
						ContactsConstants.DNAME));
				if (Integer.parseInt(Utils.getColumnValue(mCursor,
						ContactsConstants.HPN)) > 0) {
					contact.setPphone(getPhoneNumbers(id));
				}
				contact.setEmail(getEmails(id));
				contact.setAddresses(getAddresses(id));
				contact.setNotes(getNotes(id));
				contact.setImAddresses(getIms(id));
				contact.setOrganization(getOrganizations(id));
				contactList.addContacs(contact);
				android.util.Log.w("ContactsApiSdk5",
						" Enter getContactList() " + contact);
			}
		}
		return contactList;
	}

	private ArrayList<Phone> getPhoneNumbers(String id) {
		ArrayList<Phone> phones = new ArrayList<Phone>();
		Cursor phCursor = this.mContentResolver.query(ContactsConstants.PURI,
				null, ContactsConstants.CID + " = ? ", new String[] { id },
				null);
		while (phCursor.moveToNext()) {
			Phone phone = new Phone(Utils.getColumnValue(phCursor,
					ContactsConstants.PNUM), Utils.getColumnValue(phCursor,
					ContactsConstants.PHONETYPE));
			phones.add(phone);
		}
		phCursor.close();
		android.util.Log.w("ContactsApiSdk5", " Enter getPhoneNumbers() "
				+ phones);
		return phones;
	}

	private ArrayList<Email> getEmails(String id) {
		ArrayList<Email> emails = new ArrayList<Email>();
		Cursor emCursor = this.mContentResolver.query(ContactsConstants.EURI,
				null, ContactsConstants.EID + " = ? ", new String[] { id },
				null);
		while (emCursor.moveToNext()) {
			Email email = new Email(Utils.getColumnValue(emCursor,
					ContactsConstants.EMAIL), Utils.getColumnValue(emCursor,
					ContactsConstants.EMAILTYPE));
			emails.add(email);
		}
		emCursor.close();
		android.util.Log.w("ContactsApiSdk5", " Enter getEmails() " + emails);
		return emails;
	}

	private ArrayList<String> getNotes(String id) {
		ArrayList<String> notes = new ArrayList<String>();
		String where = ContactsConstants.CONTACT_ID + " = ? AND "
				+ ContactsConstants.MIME_TYPE + " = ? ";
		String[] whereParameters = new String[] { id,
				ContactsConstants.NOTE_CIT };
		Cursor notesCur = this.mContentResolver.query(ContactsConstants.DURI,
				null, where, whereParameters, null);
		while (notesCur.moveToNext()) {
			String note = Utils
					.getColumnValue(notesCur, ContactsConstants.NOTE);
			if (note.length() > 0) {
				notes.add(note);
			}
		}
		notesCur.close();
		android.util.Log.w("ContactsApiSdk5",
				" Enter getNotes() " + notes.size());
		return notes;

	}

	private ArrayList<Address> getAddresses(String id) {
		ArrayList<Address> addresses = new ArrayList<Address>();
		String where = ContactsConstants.CONTACT_ID + " = ? AND "
				+ ContactsConstants.MIME_TYPE + " = ? ";
		String[] whereParameters = new String[] { id, ContactsConstants.AID_CIT };
		Cursor addressCur = this.mContentResolver.query(ContactsConstants.DURI,
				null, where, whereParameters, null);
		while (addressCur.moveToNext()) {
			String street = Utils.getColumnValue(addressCur,
					ContactsConstants.STREET);
			String city = Utils.getColumnValue(addressCur,
					ContactsConstants.CITY);
			String state = Utils.getColumnValue(addressCur,
					ContactsConstants.STATE);
			String postalCode = Utils.getColumnValue(addressCur,
					ContactsConstants.POSTCODE);
			String country = Utils.getColumnValue(addressCur,
					ContactsConstants.COUNTRY);
			String pobox = Utils.getColumnValue(addressCur,
					ContactsConstants.POSTBOX);
			String type = Utils.getColumnValue(addressCur,
					ContactsConstants.ADDRESS_TYPE);
			Address address = new Address(street, city, state, postalCode,
					country, pobox, type);
			addresses.add(address);
			android.util.Log.w("ContactsApiSdk5", " Enter getAddresses() "
					+ address);
		}
		addressCur.close();

		return addresses;
	}

	private ArrayList<IM> getIms(String id) {
		ArrayList<IM> ims = new ArrayList<IM>();
		String where = ContactsConstants.CONTACT_ID + " = ? AND "
				+ ContactsConstants.MIME_TYPE + " = ? ";
		String[] whereParameters = new String[] { id, ContactsConstants.IM_CIT };
		Cursor imsCur = this.mContentResolver.query(ContactsConstants.DURI,
				null, where, whereParameters, null);
		if (imsCur.moveToFirst()) {
			String imData = Utils.getColumnValue(imsCur,
					ContactsConstants.IM_DATA);
			String imType;
			imType = Utils.getColumnValue(imsCur, ContactsConstants.IM_TYPE);
			if (imData.length() > 0) {
				IM im = new IM(imData, imType);
				ims.add(im);
				android.util.Log.w("ContactsApiSdk5", " Enter getIms() " + im);
			}

		}
		imsCur.close();
		return ims;
	}

	private Organization getOrganizations(String id) {
		Organization organization = new Organization();
		String where = ContactsConstants.CONTACT_ID + " = ? AND "
				+ ContactsConstants.MIME_TYPE + " = ? ";
		String[] whereParameters = new String[] { id, ContactsConstants.ORG_CIT };
		Cursor orgCur = this.mContentResolver.query(ContactsConstants.DURI,
				null, where, whereParameters, null);
		if (orgCur.moveToFirst()) {
			String orgData = Utils.getColumnValue(orgCur,
					ContactsConstants.ORG_DATA);
			String orgTitle;
			orgTitle = Utils
					.getColumnValue(orgCur, ContactsConstants.ORG_TITLE);
			if (orgTitle.length() > 0) {
				organization.setOrganization(orgData);
				organization.setTitle(orgTitle);
				android.util.Log.w("ContactsApiSdk5",
						" Enter getOrganizations() " + organization);
			}
		}
		orgCur.close();
		return organization;
	}
}
